package Plugins::SBDir2Lib::Settings;

#	----------------------------------------------------------------------
#
#	Copyright (c) 2015 Guenther Roll
#
# 	This program is free software; you can redistribute it and/or
# 	modify it under the terms of the GNU General Public License,
# 	version 2.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#	02111-1307 USA
#

use strict;
use base qw(Slim::Web::Settings);
use Storable;

use Slim::Music::VirtualLibraries;
use Slim::Utils::Log;
use Slim::Utils::Misc;
use Slim::Utils::Strings qw(string);
use Slim::Utils::Prefs;

my $prefs = preferences('plugin.SBDir2Lib');


# ----------------------------------------------------------------------------
# References to other classes
# ----------------------------------------------------------------------------
my $classPlugin	= undef;


# ----------------------------------------------------------------------------
my $log = Slim::Utils::Log->addLogCategory({
	'category'     => 'plugin.SBDir2Lib',
	'defaultLevel' => 'ERROR',
	'description'  => 'PLUGIN_SBDIR2LIB',
});


# ----------------------------------------------------------------------------
# Define own constructor
# ----------------------------------------------------------------------------
sub new {
	my $class = shift;
	$classPlugin = shift;

	$log->debug( "*** SBDir2Lib::Settings::new() " . $classPlugin . "\n");

	$class->SUPER::new();

	return $class;
}


# ----------------------------------------------------------------------------
# Name in the settings dropdown
# ----------------------------------------------------------------------------
sub name {
	return Slim::Web::HTTP::CSRF->protectName('PLUGIN_SBDIR2LIB');
}

# ----------------------------------------------------------------------------
# Webpage served for settings
# ----------------------------------------------------------------------------
sub page { #tells which file to use as the web page
	return 'plugins/SBDir2Lib/settings/sbdir2lib.html';
}

# ----------------------------------------------------------------------------
sub handler {
	my ($class, $client, $params) = @_;

	my $serverPrefs = $class->getServerPrefs($client);

	if ($params->{'saveSettings'}) {
		my $menus = $prefs->get('D2LExtraLibraries');

		for (my $i = 1; defined $params->{"id$i"}; $i++) {

			if ( $params->{"delete$i"} ) {
				Slim::Menu::BrowseLibrary->deregisterNode($params->{"id$i"});
				my $serverPrefs = preferences('server');

				$serverPrefs->remove('disabled_' . $params->{"id$i"});

				$menus = [ grep { $_->{id} ne $params->{"id$i"} } @$menus ];
				next;
			}

			my ($menu) = $params->{"id$i"} eq '_new_' ? {} : grep { $_->{id} eq $params->{"id$i"} } @$menus;

			delete $menu->{enabled} if $serverPrefs;

			next unless ( $params->{"Library$i"} && $params->{"DisplayName$i"} && $params->{"Condition$i"} && $params->{"Icon$i"} );

			if ( $params->{"id$i"} eq '_new_' ) {
				$menu = {
					id => Time::HiRes::time(),
					enabled => 1,
				};

				$params->{"id$i"} = $menu->{id};

				push @$menus, $menu;
			}

			if ($params->{"Library$i"}) {
				$menu->{params}->{Library_id} = $params->{"Library$i"}; 
			}
			else {
				delete $menu->{params}->{Library_id};
			}

			if ($params->{"DisplayName$i"}) {
				$menu->{params}->{DisplayName_id} = $params->{"DisplayName$i"}; 
			}
			else {
				delete $menu->{params}->{DisplayName_id};
			}

			if ($params->{"Condition$i"}) {
				$menu->{params}->{Condition_id} = $params->{"Condition$i"}; 
			}
			else {
				delete $menu->{params}->{Condition_id};
			}
			
			if ($params->{"Icon$i"}) {
				$menu->{params}->{Icon_id} = $params->{"Icon$i"}; 
			}
			else {
				delete $menu->{params}->{Icon_id};
			}
		}

		$prefs->set('D2LExtraLibraries', $menus);
	}

	$params->{libraries} = {};

	my $libraries = Slim::Music::VirtualLibraries->getLibraries();
	while (my ($k, $v) = each %$libraries) {
		$params->{libraries}->{$k} = $v->{name};
	}

	# my %ids;
	# $params->{menu_items} = [ map {
		# $ids{$_->{id}}++;
		# $_->{enabled} = $serverPrefs ? ($serverPrefs->get('disabled_' . $_->{id}) ? 0 : 1) : 1;
		# $_;
	# } @{Storable::dclone($prefs->get('D2LExtraLibraries'))}, { id => '_new_' } ];
	
	# unshift @{$params->{menu_items}}, map { {
		# name => $_->{name},
		# id   => $_->{id},
		# enabled => $serverPrefs ? ($serverPrefs->get('disabled_' . $_->{id}) ? 0 : 1) : 1,
	# } } sort { 
		# $a->{weight} <=> $b->{weight}
	#don't allow to disable some select browse menus
	# } grep {
	#	$_->{id} !~ /^(?:myMusicArtists|myMusicArtistsAlbumArtists|myMusicArtistsAllArtists|myMusicAlbums)$/ && 
		# !$ids{$_->{id}}
	# } @{Slim::Menu::BrowseLibrary->_getNodeList()}, { 
		# id => plugins::SBDir2Lib::Plugin->tag,
		# name => plugins::SBDir2Lib::Plugin->getDisplayName,
		# weight => plugins::SBDir2Lib::Plugin->weight,
	# } if $class->needsClient;

	$class->SUPER::handler($client, $params);
}

sub getServerPrefs {}

1;

__END__
